package edu.jmu.action.permanage;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import edu.jmu.action.BaseAction;
import edu.jmu.bean.PageView;
import edu.jmu.bean.privilege.Role;
import edu.jmu.bean.user.Depart;
import edu.jmu.bean.user.User;
import edu.jmu.service.sysmanage.RoleService;
import edu.jmu.service.user.DepartService;
import edu.jmu.service.user.UserService;

@Controller
@Scope(value="prototype")
public class UserManageAction extends BaseAction<User> {
	private static final long serialVersionUID = -6211612919472446058L;
	
	@Resource
	private UserService userService;
	@Resource
	private RoleService roleService;
	@Resource
	private DepartService departService;
	
	private List<Role> roles;
	private String[] userName;
	private List<Depart> departs;
	private User user;
	
	
	//@Permission(module="permanage",privilege="listUser",desc="展示用户列表")
	public String listUser(){
		pageView = new PageView<User>(maxresult,currentpage);
		LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
		orderBy.put("registerDate", "desc");
		pageView.setQueryResult(userService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(),orderBy));
		roles = roleService.getScrollData().getResultlist();
		departs = departService.getScrollData().getResultlist();
		return SUCCESS;
	}
	
//	@Permission(module="user",privilege="addUser",desc="添加用户")
	public String addUser(){		
		userService.save(user);
		return SUCCESS;
	}
	
//	@Permission(module="permanage",privilege="deleteUser",desc="删除用户列表")
	public String deleteUser(){
		userService.delete((Serializable[])userName);
		return SUCCESS;
	}
	
//	@Permission(module="permanage",privilege="updateUser",desc="更新用户列表")
	public String updateUser(){
		userService.update(user);
		return SUCCESS;
	}
	
//	@Permission(module="role",privilege="editRole",desc="修改角色")
	public String editRole(){
		//user.setPrivileges(new HashSet<Privilege>(privileges));
		userService.update(user);
		return SUCCESS;
	}
	
	@SkipValidation
	@Action(value="showBranch" ,results={
			@Result(location="/page/sysmanage/editRole.jsp"),
			@Result(name=INPUT,location="/page/sysmanage/editRole.jsp")
	})

	public String goAddUser(){
		roles = roleService.getScrollData().getResultlist();
		return SUCCESS;
	}
	public void setUserName(String[] userName) {
		this.userName = userName;
	}

	public List<Role> getRoles() {
		return roles;
	}
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public List<Depart> getDeparts() {
		return departs;
	}
	
	
}
